$(function(){
    $.post(webroot+'/UserRole/getLoginName.do',function(data){
    	sessionStorage.name=data;
		console.log(data);
	});
	
	
	// 菜单点击事件
	$("#ca_contanier").width((window.innerWidth - 64) + "px");
	$("#ca_contanier").height((window.innerHeight - 60) + "px");

	$(".ca_right li").on('mouseover',function(){
		$(this).css('background','rgba(0,0,0,0.2)')
	}).mouseout(function(){
		$(this).css('background','none');
	});
	
	//显示退出系统
	var tipInfo=$('<span style="position:absolute;top:50px;right:0px;color:#333;">退出系统</span>');
	$(".ion-power").parent().on('mouseover',function(){
		$(this).append(tipInfo);
	}).on('mouseout',function(){
		$(this).children('span').remove();
	});
	
	var passwordInfo=$('<span style="position:absolute;top:50px;right:60px;color:#333;">修改密码</span>');
	$(".ion-ios-gear").parent().on('mouseover',function(){
		$(this).append(passwordInfo);
	}).on('mouseout',function(){
		$(this).children('span').remove();
	});
	
	
	$.ajax({
		url: webroot + '/menu/MenuList.do',
		type: "post",
		data: {},
		success:function(data){
			var liList="";
			for(var i=0; i<data.length; i++){
				if(data[i].sysMenuUrl == "main/toHome.do"){
                    $("#ca_contanier").attr("src", webroot + "/" + data[i].sysMenuUrl);
				}
				if(data[i].sysMenuUrl != null && data[i].sysMenuUrl != ""){
					liList += '<li class="ca_home" id="'+ data[i].id +'">' +
									'<a href="' + webroot + "/" + data[i].sysMenuUrl +'" target="ca_rightiframe">' +
										'<i class="ion '+ data[i].sysMenuCode +'"></i>' +
										'<span>'+ data[i].sysMenuName +'</span>' +
									'</a>' +
							   '</li>';
				}else{
					liList += '<li class="ca_home navnext" id="'+ data[i].id +'">' +
									'<a href="javascript:void(0)">' +
										'<i class="ion '+ data[i].sysMenuCode +'"></i>' +
										'<span>'+ data[i].sysMenuName +'</span>' +
									'</a>' +
									'<ul class="childrenul clearfix"></ul>' +
							   '</li>';
				}
				
			}
			$(".ca_leftnav ul").append(liList);
			var leftHeight = $(window).height()-$(".ca_topnav").height();
			if($(".ca_leftnav ul").height()<leftHeight){
				//$(".ca_leftnav ul").height(leftHeight);
				$(".ca_leftnav").height(leftHeight);
			}else{
				$(".ca_leftnav").height($(".ca_leftnav ul").height()+10);
			}
			$(".ca_leftnav ul li").eq(0).addClass("active");
			$(".ca_leftnav ul li").on('click',function(){
				$(".ca_leftnav ul li ul").html("");
				
				var selfHeight = $(this).offset().top;
				var bodyHeight = $(document).height();
				if(selfHeight > bodyHeight/2){
					$(this).children("ul").css({
						  top: 'inherit',
					  	  bottom: 0
					});
				}
				var _self = this;
				if($(this).hasClass("navnext")){
					$.ajax({
						url: webroot + '/menu/TwoMeunList.do',
						type: "post",
						data: {id: $(this).attr("id")},
						success:function(obj){
							var nextLi = "";
                            if(obj.indexOf("<!DOCTYPE html>") != -1){
                                top.location.href = webroot;
							}else{
                                for(var j=0; j<obj.length; j++){
                                    nextLi += '<li><a href="' + webroot + "/" + obj[j].sysMenuUrl +'"  target="ca_rightiframe">'+ obj[j].sysMenuName +'</a></li>';
                                }
                                $(_self).children("ul").append(nextLi);

                                $(".childrenul li").on('click',function(e){
                                    e.stopPropagation();
                                    $(this).parent().html("");
                                });

                                $(document).on('click',function(){
                                    $(".childrenul li").parent().html("");
                                });


//							//使二级ul消失
                                $(window.frames["ca_rightiframe"].document).find("html").on('click',function(){
                                    $(".childrenul li").parent().html("");
                                });
//

                                $("#ca_contanier").on('click',function(){
                                    $(".childrenul li").parent().html("");
                                })
							}
						}
					})
				}else{
					
				}
				$(this).addClass("active").siblings().removeClass("active");
			});
		}
	});
	
	
    $(".editPad").on('click',function(){
    	
    	layer.open({
    	  title: '修改密码', 
  		  skin: 'demo-class',
  		  btn: ['确认', '取消'],
  		  area:['350px','37%'],
  		  content: $(".editPadEnter").html(),
  		  success: function(layero, index){
  			$(layero).find('#editPadform').validate({
		    	rules: {
		    		confirmPad:{
				        equalTo: $(layero).find('#editPadform').find("#newPad")
				    },
				    oldPad:{
				   		remote: {
				   			url : webroot + '/user/ifPawAgreement.ajax',
							 type:"post",
	                   	 	 dataType: "json",
							 data : { // 要传递的数据
								 oldPad: function () {
	                                return $(layero).find('#editPadform').find("#oldPad").val();
	                            }
							 }
		                }
				   }
		    	}
		    });
  		  },
		  yes:function(index, layero){
			  if($(layero).find('#editPadform').valid()){
				  $.ajax({
					  url : webroot+'/user/updatePassword.ajax',
					  data : {'newPad':$(layero).find('#editPadform').find("#newPad").val()},
					  type : 'POST',
					  dataType : 'json',
					  success : function(data){
						  layer.msg(data.msg);
						  if(data.type == '2'){
							  layer.close(index);
						  }
					  }
				  })
			  }
		  },
  		  cancel:function(index){
  			  layer.close(index);
  		  }
  		});
    });
})
